# Maintainer: Feng Shu <tumashu@163.com>

_realname=librime
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.7.3
pkgrel=1
pkgdesc="Rime Input Method Engine Library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/rime/librime"
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-leveldb"
         "${MINGW_PACKAGE_PREFIX}-marisa"
         "${MINGW_PACKAGE_PREFIX}-opencc"
         "${MINGW_PACKAGE_PREFIX}-yaml-cpp"
         "${MINGW_PACKAGE_PREFIX}-glog"
         "${MINGW_PACKAGE_PREFIX}-capnproto")
optdepends=("${MINGW_PACKAGE_PREFIX}-rime-data: Rime schema repository from plum")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gtest"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/rime/librime/archive/${pkgver}.tar.gz"
        "001-fix-librime-path.patch"
        "002-install-pkgconfig-file.patch"
        "003-cmath-include.patch")
sha256sums=('c76b937a27d7b9352c3eea9eb3adaebf70c93457104c7d47d40c006009092c20'
            'a041646654d577f71aba8c0a83b2b0ece51bf4a20fe8a2f9b22c5b3388294ad3'
            '579c21aa8dfefd9fccedd35ae29d11d9807294c2843c11d80f3aef8993da516f'
            '71c89059e5da2c7e6ccf6c6240acd2f43116f24c06b7128c469969ed2c40b35a')

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/001-fix-librime-path.patch
  patch -p1 -i ${srcdir}/002-install-pkgconfig-file.patch
  patch -p1 -i ${srcdir}/003-cmath-include.patch
}

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -G"Ninja" \
    -DBOOST_ROOT=${MINGW_PREFIX} \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -DBUILD_TEST=OFF \
    -DBOOST_USE_CXX11=ON \
    -DBUILD_STATIC=ON \
    -DBUILD_WITH_ICU=ON \
    -DENABLE_LOGGING=OFF \
    -DCMAKE_CXX_STANDARD_LIBRARIES="-lbcrypt" \
    "../${_realname}-${pkgver}"

  cmake --build .
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" cmake --install .
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
